package com.csd.fragment;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.csd.activity.PersonalDetailActivity;
import com.csd.activity.R;

public class OutCarFragment extends Fragment implements OnClickListener {

	private int minute;
	private int second;
	private int hour;
	private int minute1;
	private int second1;
	private int hour1;
	private TextView tv_hour, tv_minute, tv_second, tv_hour1, tv_minute1,
			tv_second1;
	private TimerTask timerTask, timerTask1;
	private Timer timer, timer1;
	private View v;
	private LinearLayout ll_detail;
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			if (msg.what == 0) {
				if (hour == 0 && minute == 0 && second == 0) {
					if (hour >= 10) {
						tv_hour.setText(String.valueOf(hour));
					} else {
						tv_hour.setText("0" + String.valueOf(hour));
					}
					if (minute >= 10) {
						tv_minute.setText(String.valueOf(minute));
					} else {
						tv_minute.setText("0" + String.valueOf(minute));
					}
					if (second >= 10) {

						tv_second.setText(String.valueOf(second));
					} else {
						tv_second.setText("0" + String.valueOf(second));
					}
					return;
				}
				if (second == 0 && minute == 0 && hour != 0) {
					second = 59;
					minute = 59;
					hour -= 1;
				} else if (second == 0 && hour == 0 && minute != 0) {
					second = 59;
					minute -= 1;
				} else if (second == 0 && hour != 0 && minute != 0) {

					second = 59;
					minute -= 1;
				} else {
					second--;
				}

				if (hour >= 10) {
					tv_hour.setText(String.valueOf(hour));
				} else {
					tv_hour.setText("0" + String.valueOf(hour));
				}
				if (minute >= 10) {
					tv_minute.setText(String.valueOf(minute));
				} else {
					tv_minute.setText("0" + String.valueOf(minute));
				}
				if (second >= 10) {

					tv_second.setText(String.valueOf(second));
				} else {
					tv_second.setText("0" + String.valueOf(second));
				}
			}
			if (msg.what == 1) {
				if(second1 == 59){
					second1 = 0;
					minute1 +=1;
				}else{
					second1++;
				}
				if(minute1 == 59){
					minute1 = 0;
					hour1 += 1;
				}
				
				if (hour1 >= 10) {
					tv_hour1.setText(String.valueOf(hour1));
				} else {
					tv_hour1.setText("0" + String.valueOf(hour1));
				}
				if (minute1 >= 10) {
					tv_minute1.setText(String.valueOf(minute1));
				} else {
					tv_minute1.setText("0" + String.valueOf(minute1));
				}
				if (second1 >= 10) {

					tv_second1.setText(String.valueOf(second1));
				} else {
					tv_second1.setText("0" + String.valueOf(second1));
				}
			}
		};
	};

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		v = inflater.inflate(R.layout.fragment_outcar, null);
		return v;
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		initView();
	}
	private void initView() {
		tv_hour = (TextView) v.findViewById(R.id.hour);
		tv_minute = (TextView) v.findViewById(R.id.minute);
		tv_second = (TextView) v.findViewById(R.id.second);

		tv_hour1 = (TextView) v.findViewById(R.id.hour1);
		tv_minute1 = (TextView) v.findViewById(R.id.minute1);
		tv_second1 = (TextView) v.findViewById(R.id.second1);
		ll_detail = (LinearLayout) v.findViewById(R.id.order_detail);
		ll_detail.setOnClickListener(this);
		hour = 0;
		minute = 10;
		second = 0;
		hour1 = 0;
		minute1 = 0;
		second1 = 0;

		tv_hour.setText(String.valueOf(hour));
		tv_minute.setText(String.valueOf(minute));
		tv_second.setText(String.valueOf(second));
		
		tv_hour1.setText(String.valueOf(hour1));
		tv_minute1.setText(String.valueOf(minute1));
		tv_second1.setText(String.valueOf(second1));

		timerTask = new TimerTask() {

			@Override
			public void run() {
				Message msg = new Message();
				msg.what = 0;
				handler.sendMessage(msg);
			}
		};

		timer = new Timer();
		timer.schedule(timerTask, 0, 1000);

		timerTask1 = new TimerTask() {

			@Override
			public void run() {
				Message msg = new Message();
				msg.what = 1;
				handler.sendMessage(msg);
			}
		};

		timer1 = new Timer();
		timer1.schedule(timerTask1, 0, 1000);

	}

	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub

		if (timer != null) {
			timer.cancel();
			timer = null;
		}
		if (timerTask != null) {
			timerTask = null;
		}
		minute = -1;
		second = -1;
		hour = -1;
		super.onDestroyView();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v == ll_detail){
			Intent intent = new  Intent(getActivity(), PersonalDetailActivity.class);
			startActivity(intent);
		}
	}
}
